<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item :label="$t('姓名')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" :placeholder="$t('请输入姓名')"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('手机号')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="phone">
              <a-input v-model="model.phone" :placeholder="$t('请输入手机号')"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('业务员类型')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <j-dict-select-tag
                type="list"
                v-model="model.type"
                dictCode="bis_salesman.type"
                :placeholder="$t('请选择业务员类型')"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('绑定会员')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="memberId">
              <a-auto-complete
                v-model="model.memberId"
                class="global-search"
                size="large"
                style="width: 100%"
                :placeholder="$t('请选择绑定会员')"
                option-label-prop="title"
                @select="onSelect"
                @search="searchMember"
              >
                <template slot="dataSource">
                  <a-select-option key="99999999" :label="$t('标题')" value="99999999" disabled>
                    <span style="display: inline-block; width: 120px; color: #000; font-weight: bold">{{$t('手机号')}}</span>
                    <span style="display: inline-block; width: 150px; color: #000; font-weight: bold">{{$t('姓名')}}</span>
                    <span style="display: inline-block; width: 60px; color: #000; font-weight: bold">{{$t('币值')}}</span>
                  </a-select-option>
                  <a-select-option v-for="item in memberList" :key="item.id" :value="item.id" :title="item.phone">
                    <span style="display: inline-block; width: 120px">{{ item.phone }}</span>
                    <span style="display: inline-block; width: 150px">{{ item.name }}</span>
                    <span style="display: inline-block; width: 60px">{{
                      item.memberAccount.realCoin + item.memberAccount.presentCoin
                    }}</span>
                  </a-select-option>
                </template>
                <a-input>
                  <a-button slot="suffix" style="margin-right: -12px" class="search-btn" size="large" type="primary">
                    <a-icon type="search" />
                  </a-button>
                </a-input>
              </a-auto-complete>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('绑定系统用户')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="userId">
              <j-dict-select-tag
                type="list"
                v-model="model.userId"
                dictCode="sys_user,realname,id"
                :placeholder="$t('请选择绑定系统用户')"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('状态')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="state">
              <a-select v-model="model.state" :placeholder="$t('请输入状态')">
                <a-select-option :value="1">{{$t('启用')}}</a-select-option>
                <a-select-option :value="0">{{$t('停用')}}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'BisSalesmanForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: {
        type: 0,
        state: 1,
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        name: [{ required: true, message: this.$t('请输入姓名') }],
        phone: [{ required: true, message: this.$t('请输入手机号') }],
        type: [{ required: true, message: this.$t('请输入业务员类型') }],
        memberId: [{ required: true, message: this.$t('请输入绑定会员') }],
        state: [{ required: true, message: this.$t('请输入状态') }],
      },
      url: {
        add: '/member/bisSalesman/add',
        edit: '/member/bisSalesman/edit',
        queryById: '/member/bisSalesman/queryById',
      },
      memberList: [],
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
      this.searchMember()
    },
    searchMember(value) {
      let query = {}
      query.memberInfo = value
      query.pageNo = 1
      query.pageSize = 20
      query.state = '0'
      query.anyOpenId = 1
      getAction('/member/bisMemberInfo/list', query).then((res) => {
        if (res.success) {
          this.memberList = res.result.records
        }
      })
    },
    onSelect(value){
      console.log('onSelect', value);
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>